#
# This file is part of codzo-freelance-timetracker.
# 
# codzo-freelance-timetracker is free software: you can
# redistribute it and/or modify it under the terms of the GNU
# General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your
# option) any later version.
# 
# codzo-freelance-timetracker is distributed in the
# hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
# Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with codzo-freelance-timetracker. If not, see
# <http://www.gnu.org/licenses/>.
#

# Environment 
OS      := $(shell uname -s)
PROJ    := codzo-freelance-timetracker

# environment variable WXWIN for wx-config
WXWIN   ?= .

# Compiler
CC      := g++

# Commands
MKDIR   := $(shell which mkdir) -p
RM      := $(shell which rm) -rf

# Output from wx-config --libs
# Make sure the path to wxwidget/lib/gcc_dll or like has been added
#   to LIBRARY_PATH
WXLIBS  := $(shell wx-config --libs)


# Output from wx-config --cxxflags
# Make sure the path to wxwidget/lib/gcc_dll/mswu and wxwidget/include
#   or like has been added to CPATH
WXFLAGS := $(shell wx-config --cxxflags)


# Compiler parameters
ifeq ($(OS),Linux)
OPTIONS := -Wall
else ifeq ($(OS),windows32)
OPTIONS := -Wl,-subsystem,windows -Wall
endif


# Compiler parameters for DEBUG
DEBUG   := -g

# Directory of source files
SRCDIR  := src

# Directory of resource
RESDIR  := resource

# Directory of library
LIBDIR  := lib

# Directory of .h files
INCDIR  := include

# Directory of object files
BLDDIR  := build-$(OS)

# Compiler will search for library and included files in this setting
VPATH   := $(SRCDIR) : $(INCDIR)

# Name of output file (EXE)
DEST    := $(PROJ).exe


# List of all .cpp files (no extension required)
CPPS    := \
		main/timetracker \
		main/taskbaricon \
		main/browserwindow \


# Work out a list of all source files
SRCS    := $(addprefix $(SRCDIR)/,  $(addsuffix .cpp, $(CPPS)))

# Work out a list of all object files
OBJS    := $(addprefix $(BLDDIR)/,  $(addsuffix .o, $(CPPS)))


# .RC file to be linked
RCFILE  := $(RESDIR)/app.rc
# Object file of RC resource
RCOBJ   := $(BLDDIR)/main/app-rc.o

# Default rule, build and run
default: compile 

# Logic Rule: setup the dependence between object file and it's source file
$(OBJS) : $(BLDDIR)/%.o : $(SRCDIR)/%.cpp $(INCDIR)/%.h
	$(MKDIR) $(dir $@)
	$(CC) $(OPTIONS) -c -I$(SRCDIR) -I$(INCDIR) -I$(RESDIR) $(WXFLAGS) -o $@ $< -L$(LIBDIR) $(WXLIBS)

# Logic Rule: setup the dependence between RES object file and it's artificals
$(RCOBJ): $(RCFILE) $(RESDIR)/icons/app.ico
	@echo "# Rebuild Resources"
	windres -O COFF -I$(WXWIN)/include -o $(RCOBJ) $(RCFILE) 


# For windows a RC file is required
compile-windows32 : $(OBJS) $(RCOBJ)
	@echo "# Linking Objects"
	$(CC) $(OPTIONS) $(WXFLAGS) -o $(BLDDIR)/$(DEST) $(OBJS) $(RCOBJ) -L$(LIBDIR) $(WXLIBS)

compile-Linux : $(OBJS)
	$(CC) $(OPTIONS) $(WXFLAGS) -o $(BLDDIR)/$(DEST) $(OBJS) -L$(LIBDIR) $(WXLIBS)

compile: compile-$(OS)
	@echo "# Compile done"

# Logical targets, not building files
.PHONY : clean ctags run test compile

# Remove everything
clean:
	$(RM) $(BLDDIR)
	@echo "# Cleaned"

# Reset the config file and Execute
run: 
	@$(BLDDIR)/$(DEST)

test:
	@echo $(OS)
	@echo $(WXWIN)
	@echo $(SRCS)
	@echo $(OBJS)
	@echo $(WXLIBS)
	@echo $(WXFLAGS)
